/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import java.util.Vector;
import org.w3c.dom.Document;

/**
 *
 * @author MinhNhat
 */
public class UpdateSentence extends MySentence {

    String Table;
    Vector Assignments;
    Vector Conditions;

    public UpdateSentence(String strUpdate) {       
        Assignments= new Vector();
        Conditions = new Vector();
        strUpdate = strUpdate.replaceAll("(\r\n|\n)", " ");
        
        if(!strUpdate.isEmpty())
        {
            strUpdate = GetTable(strUpdate);
        }
        
        if(!strUpdate.isEmpty())
        {    
            strUpdate = GetAssignments(strUpdate);
        }
        
        if(!strUpdate.isEmpty())
        {
            Conditions = GetConditions(strUpdate);
        }
    }

    public String GetTable(String strUpdate) {
        strUpdate = strUpdate.trim();
        String s1 = strUpdate.toLowerCase();
        String s2 = "update ";
        int indexOfTableName = s1.indexOf(s2);
        if(indexOfTableName==-1)
        {
            return strUpdate;
        }
        String core = strUpdate.substring(indexOfTableName + s2.length(), strUpdate.length()).trim();

        s1 = core.toLowerCase();
        s2 = " set ";
        int indexOfEndTableName = s1.indexOf(s2);

        if(indexOfEndTableName==-1)
        {
            return strUpdate;
        }
        Table = core.substring(0, indexOfEndTableName).trim();
        return core.substring(indexOfEndTableName, core.length()).trim();
    }

    public String GetAssignments(String strUpdate) {
        String s1 = strUpdate.toLowerCase();
        String s2 = "set ";
        int indexOfStart = s1.indexOf(s2);
        int indexOfEnd = s1.indexOf(" where ");
        if(indexOfStart!=0)
        {            
            return strUpdate;
        }
        
        if(indexOfEnd==-1)
        {
            indexOfEnd = strUpdate.length();
        }
        String core = strUpdate.substring(indexOfStart + s2.length(), indexOfEnd).trim();

        String[] gan = core.split(",");
        for (int i = 0; i < gan.length; i++) {
            MyAssignment temp = new MyAssignment(gan[i].trim());
            Assignments.add(temp);
        }

        strUpdate = strUpdate.substring(indexOfEnd, strUpdate.length()).trim();
        return strUpdate;
    }

    public static boolean isUpdateSentence(String str) {
        String s1 = str.toLowerCase().trim();
        String s2 = "update ";
        int indexOfString = s1.indexOf(s2);
        if (indexOfString == 0) {
            return true;
        }
        return false;
    }

    public boolean Excute(Document doc, Vector Tables) {
        try{
        for (int i = 0; i < Tables.size(); i++) {
            MyTable table = (MyTable) Tables.get(i);
            if (Table.compareTo(table.GetName()) == 0) {
                return table.UpdateData(Conditions, Assignments);                
            }
        }
        }catch(Exception ex)
        {
            System.out.println(ex.toString());
            return false;
        }
        return false;
    }
}
